//
//  HomeViewController.swift
//  NSAttributedString
//
//  Created by 陆承东 on 16/7/12.
//  Copyright © 2016年 陆承东. All rights reserved.
//

import UIKit

class HomeViewController: UIViewController {

    @IBOutlet weak var firstLabel: UILabel!
    @IBOutlet weak var secondLabel: UILabel!
    @IBOutlet weak var thirdLabel: UILabel!
    @IBOutlet weak var forthLabel: UILabel!
    @IBOutlet weak var fifthLabel: UILabel!
    @IBOutlet weak var sixthLabel: UILabel!
    @IBOutlet weak var seventhLabel: UILabel!
    @IBOutlet weak var eightLabel: UILabel!
    @IBOutlet weak var ninthLabel: UILabel!
    @IBOutlet weak var tenthLabel: UILabel!
    @IBOutlet weak var eleventhLabel: UILabel!
    @IBOutlet weak var twelfthLabel: UILabel!
    @IBOutlet weak var thirteenthLabel: UILabel!
    @IBOutlet weak var fourteenthLabel: UILabel!
    @IBOutlet weak var fifteenthLabel: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        var attStr = NSMutableAttributedString.init()
        
        let firstString = "first string"
        
        // 仅仅拼接一个纯粹的字符串
        attStr.appendAttributedString(NSAttributedString.init(string: firstString))
        
        firstLabel.attributedText = attStr
        
        // 给拼接的字符串设置单一的属性（设置前景色）
        attStr.addAttribute(NSForegroundColorAttributeName, value: UIColor.blueColor(), range: (attStr.string as NSString).rangeOfString(firstString))
        
        secondLabel.attributedText = attStr
        
        // 给拼接的字符串设置多个属性(加上字体和背景色)
        attStr.addAttributes([NSFontAttributeName: UIFont.systemFontOfSize(14), NSBackgroundColorAttributeName: UIColor.redColor()], range: (attStr.string as NSString).rangeOfString(firstString))
        
        thirdLabel.attributedText = attStr
        
        let secondString = "second string"
        
        attStr.appendAttributedString(NSAttributedString.init(string: secondString, attributes: [NSForegroundColorAttributeName: UIColor.greenColor(), NSFontAttributeName: UIFont.systemFontOfSize(16)]))
        // 给字符串设置删除线（必须给整个字符串均设置删除线样式，不需要的地方传0）
        attStr.addAttributes([NSStrikethroughStyleAttributeName: NSNumber.init(int: 0)], range: (attStr.string as NSString).rangeOfString(firstString))
        attStr.addAttributes([NSStrikethroughStyleAttributeName: NSNumber.init(int: Int32(NSUnderlineStyle.PatternSolid.rawValue) | Int32(NSUnderlineStyle.StyleSingle.rawValue)), NSStrikethroughColorAttributeName: UIColor.orangeColor()], range: (attStr.string as NSString).rangeOfString(secondString))
        
        forthLabel.attributedText = attStr
        
        // 添加下划线
        attStr.addAttributes([NSUnderlineStyleAttributeName: NSNumber.init(int: 0)], range: (attStr.string as NSString).rangeOfString(firstString))
        attStr.addAttributes([NSUnderlineStyleAttributeName: NSNumber.init(int: Int32(NSUnderlineStyle.PatternDashDot.rawValue) | Int32(NSUnderlineStyle.StyleDouble.rawValue)), NSUnderlineColorAttributeName: UIColor.orangeColor()], range: (attStr.string as NSString).rangeOfString(secondString))
        
        fifthLabel.attributedText = attStr
        
        // 添加字间距(给secondString添加字间距)
        attStr.addAttributes([NSKernAttributeName: NSNumber.init(int: 5)], range: (attStr.string as NSString).rangeOfString(secondString))
        
        sixthLabel.attributedText = attStr
        
        let thirdString = "third string"
        
        attStr.appendAttributedString(NSAttributedString.init(string: thirdString))
        
        // 设置阴影效果
        let shadow = NSShadow.init()
        shadow.shadowOffset = CGSizeMake(1, 1)
        shadow.shadowColor = UIColor.purpleColor()
        shadow.shadowBlurRadius = 5
        attStr.addAttribute(NSShadowAttributeName, value: shadow, range: (attStr.string as NSString).rangeOfString(thirdString))
        
        sixthLabel.attributedText = attStr
        
        // 设置文本特殊效果，取值为  NSString 对象，目前只有图版印刷效果可用，没发现效果，加上就没有文字了
//        attStr.addAttribute(NSTextEffectAttributeName, value: "third string", range: (attStr.string as NSString).rangeOfString(thirdString))
        
        seventhLabel.attributedText = attStr
        
        // 设置笔画宽度，取值为 NSNumber 对象（整数），负值填充效果，正值中空效果
        attStr.addAttribute(NSStrokeWidthAttributeName, value: NSNumber.init(int: 3), range: (attStr.string as NSString).rangeOfString(secondString))
        // 边框部分颜色，不是字体颜色，取值为 UIColor 对象
        attStr.addAttribute(NSStrokeColorAttributeName, value: UIColor.brownColor(), range: (attStr.string as NSString).rangeOfString(secondString))
        
        eightLabel.attributedText = attStr
        
        // 拼接一张图片
        let attachment = NSTextAttachment()
        attachment.image = UIImage.init(named: "ic_arrow_down_theme")
        attachment.bounds = CGRectMake(0, 0, 12, 12)
        attStr.appendAttributedString(NSAttributedString.init(attachment: attachment))
        
        ninthLabel.attributedText = attStr
        
        // 设置链接属性，传入值为 NSURL或者NSString，点击后调用浏览器打开指定URL地址（未能正常跳转）
        let forthString = "forth string"
        attStr.appendAttributedString(NSAttributedString.init(string: forthString))
        attStr.addAttribute(NSLinkAttributeName, value: NSURL.init(string: "https://www.baidu.com")!, range: (attStr.string as NSString).rangeOfString(forthString))
        
        tenthLabel.attributedText = attStr
        tenthLabel.userInteractionEnabled = true
        
        // 设置基线偏移值，取值为 NSNumber （float）,正值上偏，负值下偏
        attStr.addAttribute(NSBaselineOffsetAttributeName, value: NSNumber.init(float: 4), range: (attStr.string as NSString).rangeOfString(forthString))
        
        eleventhLabel.attributedText = attStr
        
        //  设置字形倾斜度，取值为 NSNumber （float）,正值右倾，负值左倾
        attStr.addAttribute(NSObliquenessAttributeName, value: NSNumber.init(float: 1), range: (attStr.string as NSString).rangeOfString(forthString))
        
        twelfthLabel.attributedText = attStr
        
        // 设置文本横向拉伸属性，取值为 NSNumber （float）,正值横向拉伸文本，负值横向压缩文本
        attStr.addAttribute(NSExpansionAttributeName, value: NSNumber.init(float: 1), range: (attStr.string as NSString).rangeOfString(forthString))
        
        thirteenthLabel.attributedText = attStr
        
        // 设置文字书写方向，从左向右书写或者从右向左书写
        attStr.addAttribute(NSWritingDirectionAttributeName, value: [NSNumber.init(int: Int32(NSWritingDirectionFormatType.Embedding.rawValue) | Int32(NSWritingDirection.LeftToRight.rawValue))], range: (attStr.string as NSString).rangeOfString(forthString))
        
        fourteenthLabel.attributedText = attStr
        
        // 设置文字排版方向，取值为 NSNumber 对象(整数)，0 表示横排文本，1 表示竖排文本（未能正常排版）
        attStr = NSMutableAttributedString.init(string: forthString)
        attStr.addAttribute(NSVerticalGlyphFormAttributeName, value: NSNumber.init(int: 0), range: (attStr.string as NSString).rangeOfString(forthString))
        
        fifteenthLabel.attributedText = attStr
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
